import 'package:flutter/material.dart';
import 'package:flutter_base_library/index.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:safe_manage/config/my_app_data.dart';
import 'package:safe_manage/config/my_app_routes.dart';
import 'package:safe_manage/models/index.dart';

class MyLoginController extends GetxController {
  var realmTextEditingController = TextEditingController();
  var usernameTextEditingController = TextEditingController();
  var passwordTextEditingController = TextEditingController();
  var realmIsShow = false.obs;
  var agreeSelect = false.obs;
  @override
  void onInit() {
    super.onInit();
    realmTextEditingController.text = "http://182.151.199.12:8000";
    usernameTextEditingController.text = "zhy";
    passwordTextEditingController.text = "shuxin2022";
    // realmTextEditingController.text = "http://j36d367208.qicp.vip:8000";
    // usernameTextEditingController.text = "ytadmin";
    // passwordTextEditingController.text = "shuxin2022";
    KVDataUtils kvDataUtils = KVDataUtils.getInstance();
    String? realm = kvDataUtils.getString("realm");
    if (realm != null && realm.isNotEmpty) {
      realmIsShow.value = false;
      realmTextEditingController.text = realm;
    } else {
      realmIsShow.value = true;
    }
    String? username = kvDataUtils.getString("username");
    if (username != null) {
      usernameTextEditingController.text = username;
    }
    String? password = kvDataUtils.getString("password");
    if (password != null) {
      passwordTextEditingController.text = password;
    }
  }

  void loginButtonClicked() {
    if (realmTextEditingController.text.isEmpty) {
      ToastUtils.toast("请输入域名");
      return;
    }
    if (usernameTextEditingController.text.isEmpty) {
      ToastUtils.toast("请输入用户名");
      return;
    }
    if (passwordTextEditingController.text.isEmpty) {
      ToastUtils.toast("请输入密码");
      return;
    }
    if (!agreeSelect.value) {
      ToastUtils.toast("请阅读并同意服务协议与隐私政策");
      return;
    }
    loginPostNet();
  }

  void loginPostNet() {
    EasyLoading.show();
    AppData.instance.baseUrl = realmTextEditingController.text;
    HttpUtils.post(
      "/safety-web/sysUser/login",
      body: {
        "parameter": {
          "dlmc": usernameTextEditingController.text,
          "dlmm": passwordTextEditingController.text,
        }
      },
      success: (result) {
        EasyLoading.dismiss();
        MyAppData.instance.currentUser = User.fromJson(result['data']);
        KVDataUtils kvDataUtils = KVDataUtils.getInstance();
        kvDataUtils.setString(
          "realm",
          realmTextEditingController.text,
        );
        kvDataUtils.setString(
          "username",
          usernameTextEditingController.text,
        );
        kvDataUtils.setString(
          "password",
          passwordTextEditingController.text,
        );
        kvDataUtils.setString(
          "token",
          result['data']['dlmm'],
        );
        AppData.instance.token = result['data']['dlmm'];
        Get.offAllNamed(MyAppRoutes.baseTabBar);
      },
      fail: (errorMsg) {
        EasyLoading.dismiss();
      },
    );
  }

  void agreementClicked() {
    Get.toNamed(MyAppRoutes.privacyAgreement);
  }
}
